<template>
  <fix-area ref="fixArea">
    <div style="width: 100%; display: flex; justify-content: space-between; align-items: flex-start">
      <form-block ref="form" :model="form">
        <el-form-item label="短剧名称" prop="name">
          <h3 style="margin: 0;padding: 0">{{ shortPlayDetails.name ? shortPlayDetails.name : '暂未设置短剧名称' }}</h3>
        </el-form-item>
      </form-block>
      <el-button type="primary" @click="createVisible = true">添加</el-button>
    </div>
    <div style="flex: 1; min-height: 0; width: 100%">
      <table-block :loading="loading" :data="episodeList">
        <table-column label="剧集名称" width="220px" prop="name" fixed="left" />
        <table-column label="剧集数" width="220px" prop="episode_number" />
        <table-column label="剧集图标" width="120px" prop="icon" :use-slot="true">
          <template #default="{row}">
            <div style="cursor: pointer" @click="$refs.fixArea.previewImg(row.icon)">
              <cell-media :url="row.icon" />
            </div>
          </template>
        </table-column>
        <table-column label="剧集视频" width="120px" prop="icon" :use-slot="true">
          <template #default="{row}">
            <div style="cursor: pointer" @click="$refs.fixArea.previewVideo(row.video)">
              <cell-media :url="row.video" type="video" />
            </div>
          </template>
        </table-column>
        <table-column label="剧集简介" width="320px" prop="introduce" />
        <table-column label="收藏数量" width="220px" prop="collect_number" />
        <table-column label="点赞数量" width="220px" prop="like_number" />
        <table-column label="评论数量" width="220px" prop="comment_number" />
        <table-column label="状态" width="120px" prop="status" :use-slot="true">
          <template #default="{row}">
            <el-tag :type="row.status === 0 ? 'success' : 'danger'" size="small">{{ row.status === 0 ? '显示' : '隐藏' }}</el-tag>
          </template>
        </table-column>
        <table-column label="操作" width="220px" :use-slot="true" fixed="right">
          <template #default="{row}">
            <el-button size="mini" type="warning" @click="editEpisode(row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteEpisode(row.id)">删除</el-button>
          </template>
        </table-column>
      </table-block>
    </div>
    <pagination :current-page="form.page" :total="total" @current-change="currentChange" />
    <create v-if="createVisible" :visible="createVisible" :short-id="id" @cancel="createVisible = false" @enter="createEnter" />
    <edit v-if="editVisible" :visible="editVisible" :episode-id="episodeId" @cancel="editVisible = false" @enter="editEnter" />
  </fix-area>
</template>
<script>
import { shortPlayDetails, episodeList, deleteEpisode } from '@/api/short'
import Create from '@/views/short/episode/component/Create.vue'
import Edit from '@/views/short/episode/component/Edit.vue'

export default {
  name: 'Episode',
  components: {
    Create, Edit
  },
  props: {
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      form: {
        short_id: this.id,
        page: 1
      },
      total: 0,
      shortPlayDetails: {
        name: ''
      },
      createVisible: false,
      episodeList: [],
      editVisible: false,
      episodeId: ''
    }
  },
  async mounted() {
    try {
      this.loading = true
      const shortPlayDetailsRes = await shortPlayDetails({ short_id: this.id })
      this.shortPlayDetails = shortPlayDetailsRes.data
      try {
        this.loading = true
        const episodeListRes = await episodeList(this.form)
        this.episodeList = episodeListRes.data.data
        this.total = episodeListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    } catch (error) {
      this.$errorMessage()
      this.loading = false
    }
  },
  methods: {
    async deleteEpisode(episodeId) {
      try {
        await this.$warningConfirm('确认删除?')
        try {
          this.loading = true
          await deleteEpisode({ epsiode_id: episodeId })
          try {
            this.loading = true
            const episodeListRes = await episodeList(this.form)
            this.episodeList = episodeListRes.data.data
            this.total = episodeListRes.data.total
            this.loading = false
          } catch (error) {
            this.$errorMessage()
            this.loading = false
          }
        } catch (error) {
          this.$errorMessage()
          this.loading = false
        }
      } catch (error) {
        console.log('删除取消')
      }
    },
    editEpisode(episodeId) {
      this.episodeId = episodeId
      this.editVisible = true
    },
    async editEnter() {
      this.editVisible = false
      try {
        this.loading = true
        const episodeListRes = await episodeList(this.form)
        this.episodeList = episodeListRes.data.data
        this.total = episodeListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    },
    async createEnter() {
      this.createVisible = false
      try {
        this.loading = true
        const episodeListRes = await episodeList(this.form)
        this.episodeList = episodeListRes.data.data
        this.total = episodeListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    },
    async currentChange(event) {
      try {
        this.loading = true
        this.form.page = event
        const episodeListRes = await episodeList(this.form)
        this.episodeList = episodeListRes.data.data
        this.total = episodeListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    }
  }
}
</script>
